package com.ys.service.amazonorder.impl;

import java.math.BigDecimal;

import com.ys.entity.business.TpAmazonSeller;
import com.ys.entity.sys.TsUserChange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.entity.amazonorder.TcoDayShop;
import com.ys.entity.business.TpAmazonShop;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.enume.finance.TcwUserPointRunningEnum;
import com.ys.mapper.amazonorder.AmazonorderMapper;
import com.ys.service.amazonorder.TcoDayShopService;
import com.ys.service.finance.TcwAccountCountForUserService;
import com.ys.service.finance.TcwAccountCountForUserService.ChangePointDto;
import com.ys.service.finance.TcwAccountCountForUserService.ChangePointFrozenDto;

@Service("tcoDayShopService")
public class TcoDayShopServiceImpl implements TcoDayShopService {
    private static final Logger logger = LoggerFactory.getLogger(TcoDayShopServiceImpl.class);

	@Autowired
	private AmazonorderMapper amazonorderMapper;
    
    @Autowired
    private TcwAccountCountForUserService tcwAccountCountForUserService;

	@Override
	@Transactional
	public void addAndUpdateTcoDayShop(String day) {
		amazonorderMapper.deleteBatchTcoDayShop(day);
		amazonorderMapper.insertBatchTcoDayShop(day);
		amazonorderMapper.updateBatchTcoDayShop(day);
        logger.info("完成店铺每日统计时间---" + day);
		
	}
    
    @Override
    @Transactional
    public void settleShopOrderRewardPoint(Long id, BigDecimal baseNormal, BigDecimal baseJingpin) throws Exception {
    	
    	TcoDayShop entity = new TcoDayShop().setId(id).queryFirst();
    	
    	TpAmazonShop shop = new TpAmazonShop().setId(entity.getShopId()).queryFirst();
    	if (shop == null) {
			logger.error("店铺不存在");
			return;
		}
    	//查询卖家管理
		TpAmazonSeller tpAmazonSeller = new TpAmazonSeller().setSellerId(shop.getSellerId()).queryFirst();

    	ThrOperateCompanyApply company = new ThrOperateCompanyApply().setVgsbm(tpAmazonSeller.getVgsbm()).queryFirst();
    	if (company == null) {
			logger.error("运营公司不存在");
			return;
		}
    	
    	// 发放普品奖励
    	if (entity.getNumOrderNormal() > 0) {
    		{
            	String itemName = "学堂考核";
            	String itemNameSub = "日普品订单";
            	Integer type = TcwUserPointRunningEnum.Type.AMAZONORDER_REWARD_NORMAL.value;
    			Long relativeId = entity.getId();
    			BigDecimal pointChange = baseNormal.multiply(new BigDecimal(entity.getNumOrderNormal())).negate();
    			String remark = entity.getTimeDay();
    			tcwAccountCountForUserService.changePoint_JIAO_WU_CHU(itemName, itemNameSub, type, relativeId, pointChange, remark);
    		}
    		{
				TsUserChange tsUserChange = new TsUserChange().setId(company.getOperatorChangeId()).setOrderby("A.Id DESC").queryFirst();
            	String item = "学堂考核";
            	String itemSub = "日普品订单";
            	Integer type = TcwUserPointRunningEnum.Type.AMAZONORDER_REWARD_NORMAL.value;
    			Long relativeId = entity.getId();
                String userId = tsUserChange.getUserId();
                String userName = tsUserChange.getUserName();
                BigDecimal pointChange = baseNormal.multiply(new BigDecimal(entity.getNumOrderNormal()));
                String remark = entity.getTimeDay();
    			ChangePointDto changePointDto = new ChangePointDto(item, itemSub, type, relativeId, userId, userName, pointChange, remark);
    			ChangePointFrozenDto changePointFrozenDto = null;
    			tcwAccountCountForUserService.change(changePointDto, changePointFrozenDto);
    		}
		}

    	
    	// 发放精品奖励
    	if (entity.getNumOrderJingpin() > 0) {
    		{
            	String itemName = "学堂考核";
            	String itemNameSub = "日精品订单";
            	Integer type = TcwUserPointRunningEnum.Type.AMAZONORDER_REWARD_JINGPIN.value;
    			Long relativeId = entity.getId();
    			BigDecimal pointChange = baseJingpin.multiply(new BigDecimal(entity.getNumOrderJingpin())).negate();
    			String remark = entity.getTimeDay();
    			tcwAccountCountForUserService.changePoint_JIAO_WU_CHU(itemName, itemNameSub, type, relativeId, pointChange, remark);
    		}
    		{
				TsUserChange tsUserChange = new TsUserChange().setId(company.getOperatorChangeId()).setOrderby("A.Id DESC").queryFirst();
            	String item = "学堂考核";
            	String itemSub = "日精品订单";
            	Integer type = TcwUserPointRunningEnum.Type.AMAZONORDER_REWARD_JINGPIN.value;
    			Long relativeId = entity.getId();
				String userId = tsUserChange.getUserId();
				String userName = tsUserChange.getUserName();
                BigDecimal pointChange = baseJingpin.multiply(new BigDecimal(entity.getNumOrderJingpin()));
                String remark = entity.getTimeDay();
    			ChangePointDto changePointDto = new ChangePointDto(item, itemSub, type, relativeId, userId, userName, pointChange, remark);
    			ChangePointFrozenDto changePointFrozenDto = null;
    			tcwAccountCountForUserService.change(changePointDto, changePointFrozenDto);
    		}
		}
    }
}
